package com.scn.base.page;
import java.io.Serializable;
import java.lang.reflect.Field;
import java.util.ArrayList;

import org.apache.log4j.Logger;


public class ValueObject implements Serializable {

	private static final long serialVersionUID = 2473272572839691729L;
	
	private boolean enableDebug = true; //可以使用配置文件配置该项
	private Logger log=Logger.getLogger(this.getClass());
	public Object clone() {
		try {
			return super.clone();
		}
		catch(CloneNotSupportedException e) {
			return null;
		}
	}
	
	@Override
	public String toString() {
		// TODO 自动生成方法存根
		return printEachPropertyValue();
	}
	
	//打印出类中值域的值。
	public String printEachPropertyValue() { 
		String ret ="";
		
		if( ! enableDebug) { 
			ret =super.toString() ;
			System.out.println( ret );
			return ret;
		}
		
		ret = "<-----开始" + this.getClass().getName() + "字段输出值-----";
		ArrayList fieldlist = new ArrayList();
			
		try {
			Object a = this;
			while (!a.getClass().getName().endsWith("Object")) {
				Field[] fieldArray = a.getClass().getDeclaredFields();
				for (int i = 0 ; i < fieldArray.length ; i++) {
					fieldlist.add(fieldArray[i]);
				}
				a = a.getClass().getSuperclass().newInstance();
			}
			for (int i = 0 ; i < fieldlist.size() ; i++) {
				( (Field)fieldlist.get(i) ).setAccessible(true);
				ret +="\n" + ((Field)fieldlist.get(i)).getName() +" = " + ((Field)fieldlist.get(i)).get(this);
			}
		}
		catch(Exception e) {
			e.printStackTrace();
			log.debug(e.getMessage());
		}
		ret += "\n-----结束" + this.getClass().getName() + "字段输出值----->";
		System.out.println(ret);
		return ret;
	}
	
	/**
	 * 格式化日志数据
	 * @return
	 */
	public String formatToString() { 
		String ret ="";
		
		
		ArrayList fieldlist = new ArrayList();
			
		try {
			Object a = this;
			while (!a.getClass().getName().endsWith("Object")) {
				Field[] fieldArray = a.getClass().getDeclaredFields();
				for (int i = 0 ; i < fieldArray.length ; i++) {
					fieldlist.add(fieldArray[i]);
				}
				a = a.getClass().getSuperclass().newInstance();
			}
			for (int i = 0 ; i < fieldlist.size() ; i++) {
				( (Field)fieldlist.get(i) ).setAccessible(true);
				ret +=((Field)fieldlist.get(i)).getName() +"=" + ((Field)fieldlist.get(i)).get(this)+"#";
			}
		}
		catch(Exception e) {
			e.printStackTrace();
			log.debug(e.getMessage());
		}
		//ret += "\n-----结束" + this.getClass().getName() + "字段输出值----->";
		//System.out.println(ret);
		return ret;
	}

	/**
	 * 将对象中成员变量是String的进行trim
	 */
	public void trimEachStringValue() {
		Class c = this.getClass();
		Field[] f = c.getDeclaredFields();
		try {
			for (int i = 0;i < f.length;i++ ) {
				f[i].setAccessible(true);
				if (f[i].get(this) != null) {
					if(f[i].get(this).getClass().getName().indexOf("String")>=0){
						String  s = ((String)f[i].get(this)).trim();
						f[i].set(this,s);
					}
				}
			}
		}
		catch(Exception e){
			e.printStackTrace();
			log.debug(e.getMessage());
		}	
	}

	/**
	 * 将对象中成员变量是null的转换为空(即"")
	 */
	public void nullConvertEmpty() {
		Class c = this.getClass();
		Field[] f = c.getDeclaredFields();
		try{
			for(int i = 0;i < f.length;i++){
				f[i].setAccessible(true);
				//System.out.println(" the f[i].getType().getName() is : "+f[i].getType().getName());
				if(f[i].getType().getName().indexOf("String")>=0){
					if(f[i].get(this)==null || ((String)f[i].get(this)).trim().equals("null")){
						f[i].set(this,"");
						//System.out.println(" the f[i].get(this) is : "+f[i].get(this));
					}
				}
			//f[i].setAccessible(false);
			}
		}
		catch(Exception e) {
			e.printStackTrace();
			log.debug(e.getMessage());
		}
	}
	
	

	/**
	 * 将对象中成员变量是null的转换为空格(即" ")
	 */
	public void nullConvertSpace(){
		Class c = this.getClass();
		Field[] f = c.getDeclaredFields();
		try {
			for(int i =0;i<f.length;i++){
				f[i].setAccessible(true);
				//System.out.println(" the f[i].getType().getName() is : "+f[i].getType().getName());
				if(f[i].getType().getName().indexOf("String")>=0){
					if(f[i].get(this)==null){
						f[i].set(this," ");
						//System.out.println(" the f[i].get(this) is : "+f[i].get(this));
					}
				}
				//f[i].setAccessible(false);
			}
		}
		catch(Exception e) {
			e.printStackTrace();
			log.debug(e.getMessage());
		}
	}
	
	/**
	 * 将对象中成员变量是""的转换为空格(即null)
	 */
	public void spaceConvertNull(){
		Class c = this.getClass();
		Field[] f = c.getDeclaredFields();
		try {
			for(int i =0;i<f.length;i++){
				f[i].setAccessible(true);
				//System.out.println(" the f[i].getType().getName() is : "+f[i].getType().getName());
				if(f[i].getType().getName().indexOf("String")>=0){
					if(f[i].get(this)!=null && f[i].get(this).equals("")){
						f[i].set(this,null);
						//System.out.println(" the f[i].get(this) is : "+f[i].get(this));
					}
				}
				//f[i].setAccessible(false);
			}
		}
		catch(Exception e) {
			e.printStackTrace();
			log.debug(e.getMessage());
		}
	}
	
	/**
	 * @deprecated 禁用
	 * @param enable
	 */
	public void setEnableDebug(boolean enable){
		this.enableDebug=enable;
	}
}
